﻿//导入表定义
document.write("<script language='javascript' src='/resources/js/views/commonFunction.js'></script>");
document.write("<script language='javascript' src='/resources/js/views/defineTable.js'></script>");
document.write("<script language='javascript' src='/resources/js/views/defineInit.js'></script>");
document.write("<script language='javascript' src='/resources/js/preAlarm/preAlarmCommon.js'></script>");

var action_area_allCity = "/area/query/selfCity.do"; //-- 城市
var action_prealarm_area = "/report/prealarm/area.do";	//Long areaid, String iname, Integer indicatorType, String years, Integer page, Integer pageSize
var host_path_action_department_query_city = '/department/query/city.do';

var iHeight = document.documentElement.clientHeight-200;//判断表格自适应高度
var page = 1;
var pageSize = 200;

//清除图形and表格数据
function clearDataCity(){
	$("#view_containerzhuzi").html('');   //清空图形
	$("#view_containerzhuzi").height("0px");
	$("#view_containerzhuzi").width("0px");
	$('#tt').datagrid({data:[]});
}

function query(curPage, pageSize) {

	curPage = parseInt(curPage);
	pageSize = parseInt(pageSize);
	$('#page').val(curPage);
	$('#pageSize').val(pageSize);

	var reportShowColumns = getreportShowColumns();
	var departIds= $('#annualAnalysis_departmentSelect').combobox("getValues")+ "";
	var indicatorType = $('#indicator_type_select').combobox("getValues")+ "";    //领域编号
	indicatorType=indicatorType==""?"-1":indicatorType;
	var areaid= $('#city_departmentSelect').combobox("getValue");    //地域域编号
	areaid=areaid==""?"-1":areaid;
	var iname = $('#iname').textbox('getValue');

	var years=$('#comboboxYear').combobox('getText');
	var yearList= [];
	if(years!=null ||years!='' || years!=undefined){
		yearList=years.split(',');
	}

	var formid=$("#formid").combobox("getValue");
	$.get(action_prealarm_area,
		{"iname":iname, "departId": departIds, "indicatorType": indicatorType, "areaid": areaid, "years": years, "page": curPage, "pageSize": pageSize,"formsid":formid}, function (json) {
		var code=json.code;
	    if(code!=200){
			clearDataCity();
			return;
	    }
		var data = json.data;
		var total = data["size"];
		if(total==""){
			$('#tt').datagrid({data:[]});
			$("#view_containerzhuzi").height("0px");
			$("#view_containerzhuzi").width("0px");
			return;
		}
		var totalPage = Math.ceil(total / pageSize);
		var start = (curPage - 1) * pageSize;
		var dataLength = 0;

		data = data["results"];
		if (data != null && data.length > 0) {
			dataLength = data.length;
		}

		$('#tt').datagrid( {
			height:iHeight,
			frozen:true,
			fitColumns: true,
			striped: true,  //显示条纹，隔行换色
			columns: [reportShowColumns],
			singleSelect: true,// 是否单选
			loadMsg: '数据加载中,请稍后......',
			pageNumber: curPage,//显示数据列用
			pageSize: pageSize,
			pageList: [200, 500, 1000],
			rownumbers: true,
			pagination: true,//打开翻页功能
			sortable:true,
			sortName:"code",//排序
			multiSort:true, //定义是否允许多列排序
			remoteSort:false,//表示前端排序不请求服务器
			sortOrder:'asc',
			data: data
		});

		var p = $('#tt').datagrid('getPager');
		$(p).pagination({
			total: total,
			pageList: [200, 500, 1000],
			pageSize: pageSize,
			pageNumber: curPage,
			beforePageText: '第',
			afterPageText: '页    共 ' + totalPage + ' 页',
			displayMsg: '当前显示 ' + (start + 1) + '-' + (start + dataLength) + ' 条记录 共 ' + total + ' 条记录',
			onSelectPage: function (curPage, pageSize) {
                query(curPage, pageSize,true,show.a);
			},
			onChangePageSize: function (ps) {
				pageSize = ps;
			}
		});
		try{window.parent.unmask();}catch(err){}
		//画图
		checkChangeImage(data, yearList);
	}, "json");
}

//------------------------------------------------------------------地市区域+地市部门二级联动-------------------------------------------------------------
function loadAreaAllCity() {
	$.get(action_area_allCity, function(json) {

		var years = $('#comboboxYear').combobox('getText');
		var yearList = [];
		if(years!=null ||years!='' || years!=undefined){
			yearList=years.split(',');
		}

		$('#city_departmentSelect').combobox({
			async : false,
			method : 'GET',
			data : json.data,
			editable : true,
			valueField : 'id',
			textField : 'name',
			onLoadSuccess : function() {
				var a = $('#city_departmentSelect').combobox('getData');
				if (a.length > 0) {
					$('#city_departmentSelect').combobox('select',a[0].id);
				}
				loadIndicatorTypeAll();
			},
			onSelect : function(city) {
				$.get(host_path_action_department_query_city+ '?areaid='+ city.id,function(json) {
					json.data.unshift({id : -1,name : "全部",sname : "全部"});
					$('#annualAnalysis_departmentSelect').combobox({
						async : false,
						method : 'GET',
						data : json.data,
						valueField : 'id',
						textField : 'name',
						fitColumns:false,
						collapsible: true,
						singleSelect : false,// 是否单选
						checkOnSelect: true,//选中行后,checkbox选中
						selectOnCheck: true,//选中行后,checkbox选中
						multiple:true,
						editable:false,
						formatter:function(row){
							var opts;
							if(row.selected==true){
								opts = "<input type='checkbox' checked='checked' id='"+row.id+"' value='"+row.id+"'>"+row.name+"</input>";
							}else{
								opts = "<input type='checkbox' id='"+row.id+"' value='"+row.id+"'>"+row.name+"</input>";
							}
							return opts;
						},
						onLoadSuccess : function() {
							var b = $('#annualAnalysis_departmentSelect').combobox('getData');
							if (b.length > 0) {
								$('#annualAnalysis_departmentSelect').combobox('select',b[0].id);
							}
						},
						onSelect : function() {
                            query(page,pageSize);
							showImage((yearList.length) - 1, "PAGETYPE_CITY");
						}
					});
				}, "json")
			}
		})
	}, "json");
}

var show = {
    tableshow:function () {
        show.a = !show.a;
        if(show.a){
            $("#view_chart").panel("expand",true);
            $("#tt").datagrid("resize",{
                height:iHeight
            });
        }
    },
    photoshow:function () {
        var iHeight = document.documentElement.clientHeight-128;
            $("#tt").datagrid("resize",{
                height:iHeight
            });
        show.a = false;
    },
	 chartshow:function(){
        $("#tt").datagrid("resize",{
            height:iHeight
        });
        show.a = true;
    }
};

//复选框点击事件
function checkyears(){
    query(page,pageSize);
}


$(document).ready(function () {
	$("#tabDiv").height(iHeight+40);
	$(".panel-body").height('100%');
		$('#tablepanel').panel({
		title: '数据列表',
	});
	var iH = document.documentElement.clientHeight-485;
		$('#tt').datagrid( {
			width : '100%',
			height : iHeight
	});

    // 页面加载之后再执行其他的！！！
    window.onload = function (ev) {
		loadYear();
        $('#tabDiv').tabs({
            height: 700,
            onSelect: function (title, index) {
                if (index == 0) {
                    /*	var time= setTimeout(function(){

                        },10);*/
                } else if (index == 1) {
                    var time = setTimeout(function () {
                        showData(series, jsonXData);
                        showImage(0, 'PAGETYPE_CITY')
                    }, 10);
                }
            }
        });
        $("#tabDiv").width($("#content").width());
        // try {
        //     window.parent.mask();
        // } catch (err) {
        // }
        CRUDCity.queryList();
    }
});

function inQuery(){
    query(page,pageSize);
}

var CRUDCity = {
	queryList: function () {
		loadAreaAllCity();
	}
};
var show = {
	tableshow:function () {
	  // alert(1);
	},
	photoshow:function () {
		//alert(2);
	},
	chartshow:function(){
		var time= setTimeout(function(){
			showData(series, jsonXData);
		},10);
	}
};

/*
 * rgb颜色转换
 */
function zero_fill_hex(num, digits) {
  var s = num.toString(16);
  while (s.length < digits)
	s = "0" + s;
  return s;
}


function rgb2hex(rgb) {

  if (rgb.charAt(0) == '#')
	return rgb;

  var ds = rgb.split(/\D+/);
  var decimal = Number(ds[1]) * 65536 + Number(ds[2]) * 256 + Number(ds[3]);
  return "#" + zero_fill_hex(decimal, 6);
}